package com.szml.user.controller;

import com.szml.user.result.PageResult;
import com.szml.user.result.Result;
import com.szml.user.dto.*;
import com.szml.user.service.UserService;
import com.szml.user.vo.ResetPasswordVO;
import com.szml.user.vo.UserInfoVO;
import com.szml.user.vo.UserUpdateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author limin
 * @date 2025-06-19 11:44
 * @description
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserRegisterDTO userRegisterDTO, HttpServletRequest request){
        return userService.register(userRegisterDTO,request);
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody UserLoginDTO userLoginDTO,HttpServletRequest request){
        return userService.login(userLoginDTO,request);
    }

    /**
     * 分页展示用户信息
     * @param userPageQueryDTO
     * @param token
     * @return
     */
    @GetMapping("/users")
    public Result<PageResult> getUserList(
            @ModelAttribute UserPageQueryDTO userPageQueryDTO,
            @RequestHeader("Authorization") String token){

        PageResult pageResult = userService.userList(userPageQueryDTO,token);
        return Result.success(pageResult);
    }

    /**
     * 根据userId查看用户信息
     * @param userId
     * @param token
     * @return
     */
    @GetMapping("/{userId}")
    public Result<UserInfoVO> getUserInfo(@PathVariable Long userId,
                                          @RequestHeader("Authorization") String token){
       return userService.getUserInfo(userId,token);
    }


    /**
     * 根据userId修改用户信息
     * @param userId
     * @param userUpdateDTO
     * @param token
     * @return
     */
    @PutMapping("/{userId}")
    public Result<UserUpdateVO> updateUserInfo(@PathVariable Long userId,
                                               @RequestBody UserUpdateDTO userUpdateDTO,
                                               @RequestHeader("Authorization") String token,
                                               HttpServletRequest request){
        return userService.updateUserInfo(userId,userUpdateDTO,token,request);
    }

    /**
     * 重置密码（分权）
     * @param resetPasswordDTO
     * @param token
     * @return
     */
    @PostMapping("/reset-password")
    public Result<ResetPasswordVO> resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO,
                                                 @RequestHeader("Authorization") String token,HttpServletRequest request){
        return userService.resetPassword(resetPasswordDTO,token,request);
    }

}
